import { returnPack } from './config.contants'

export type ResultDataType = Partial<ReturnType<typeof returnPack>>

/**
 * 返回值限定
 */
export class ResultData {
  resultData: Required<ResultDataType>

  constructor(resultData: Required<ResultDataType>) {
    this.resultData = resultData
  }


  static ok(result: Omit<ResultDataType, 'code'>): ResultData {
    return new ResultData({ code: CODE.SUCCESS, msg: result.msg || '', data: result.data || {} })
  }

  static msg(result: Omit<ResultDataType & { msg: string }, 'code'>): ResultData {
    return new ResultData({ code: CODE.MSG, msg: result.msg || '', data: result.data || {} })
  }

  static fail(result: Omit<ResultDataType, 'code'>): ResultData {
    return new ResultData({ code: CODE.WARNING, msg: result.msg || 'fail', data: result.data || {} })
  }
}

export enum CODE {
  // 成功
  SUCCESS = 200,

  MSG = 999,

  // 用户的错误
  WARNING = 203,
}
